//
//  StatusPictureView.swift
//  MyWeibo
//
//  Created by tanchao on 15/8/6.
//  Copyright © 2015年 chaoren. All rights reserved.
//

import UIKit
import SDWebImage
class StatusPictureView: UICollectionView {

    var status:Status?{
        didSet{
            sizeToFit()
            reloadData()
        }
    }
    private let pictureViewlayout = UICollectionViewFlowLayout()
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: pictureViewlayout)
        backgroundColor = UIColor.whiteColor()
        self.dataSource = self
        registerClass(WeiboPictureViewCell.self, forCellWithReuseIdentifier: "weibopicCell")
    }
    override func sizeThatFits(size: CGSize) -> CGSize {
        return culvViewSize()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func culvViewSize()->CGSize{
    
        let itemSize = CGSize(width: 90, height: 90)
        let margin:CGFloat = 10
        let maxCol = 3
        pictureViewlayout.itemSize = itemSize
        let count = status?.picURLS?.count ?? 0
        if  count == 0{
            return CGSizeZero
        }
        if count == 1{
            let key = status?.picURLS?.first?.absoluteString
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(key)
            var size = CGSize(width: 150, height: 120)
            if image != nil{
                size = image.size
            }
            size.width = size.width < 40 ? 40 : size.width
            size.width = size.width > UIScreen.mainScreen().bounds.width+30 ? 150 : size.width
//            size.width = size.width > 280 ? 150 : size.width
            pictureViewlayout.itemSize = size
//             print("1==>status:\(status?.user?.name) : w = \(size.width)")
            return  size
        }
        if count == 4{
            let w = itemSize.width * 2 + margin
//             print("4 ==>status:\(status?.user?.name) : w = \(w)")
            return  CGSize(width: w, height: w)
        }
        // 计算行数
        let row = (count - 1) / maxCol + 1
        let w = itemSize.width * CGFloat(maxCol) + margin * CGFloat(maxCol - 1)
        let h = itemSize.height * CGFloat(row) + margin * CGFloat(row - 1)
//        print("\(count) ==>status:\(status?.user?.name) : w = \(w)")
        return CGSize(width: w, height: h)
    }
}
extension StatusPictureView : UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.picURLS?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("weibopicCell", forIndexPath: indexPath) as! WeiboPictureViewCell
//        cell!.backgroundColor = UIColor.redColor()
        cell.imageURL = status!.picURLS![indexPath.item]

        return cell
    }
}
private class WeiboPictureViewCell: UICollectionViewCell {
    var imageURL:NSURL? {
        didSet{
            imageView.sd_setImageWithURL(imageURL!)

        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(imageView)
        imageView.ff_Fill(contentView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private lazy var imageView : UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
        }()
}
